#ifndef CODE_H
#define CODE_H

#include <QFile>

#include "chess.h"

namespace Code {        // 该名字空间负责文件读写、信息处理和表格代码转换

    using std::optional;

    struct No_start_error {};       // 缺失开始标记异常
    struct No_end_error {};     // 缺失结束标记异常
    struct Full_error {};       // 棋子加满
    struct Del_king_error {};       // 删除将帅

    class Table : public Base::Base_table {
    public:
        explicit Table(const QString& file =file_table, const QString& pre =file_pre, const QString& end =file_end) : Base_table{pre,end}, file{file}, pre_code_file{pre}, end_code_file{end} { read(read_all(file)); }
        QString text() const;       // 转为纯文本
        void read(const QString& table);        // 从字符串中读取表格信息
        void write() const;     // 保存到文件
        const QString& file_name() const { return file; }
        Base_table select(const Selector& selector) const;
    private:
        QString file;
        QString pre_code_file;
        QString end_code_file;
        QString pre_text;
        QString end_text;
    };

    class Pieces : public Base::Base_pieces {       // Pieces类是一个查询子力状态储存器
    public:
        Pieces(bool red) : red{red}, accurate{} { clear(); }
        void add_piece(QChar piece);        // 增加一颗指定棋子
        void del_piece(QChar piece);        // 删除指定棋子
        void set_accurate(bool acc) { accurate = acc; }
        bool is_accurate() const { return accurate; }
        void clear();       // 清空子力
        bool is_clear() const { return chess_king.contains(const_pieces().front().first); }
    private:
        bool red;
        bool accurate;
    };

    struct Selector {       // Selector结构体是用户定义的选择器
        Selector() : attack{true}, defence{false}, min_diff{}, max_diff{4}, win{true}, draw{true}, disputed{true} { }
        Pieces attack;
        Pieces defence;
        int min_diff;
        int max_diff;
        bool win;
        bool draw;
        bool disputed;
    };

}

#endif // CODE_H
